package com.five.api.service.impl;

import com.five.api.dao.UserMapper;
import com.five.api.dao.WalletRecordMapper;
import com.five.api.service.WalletRecordService;
import com.five.entity.WalletRecords;
import com.five.exceptions.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-21 14:49
 *
 * 钱包记录 service层实现类
 */
@Service
public class WalletRecordServiceImpl implements WalletRecordService {

    @Autowired
    private WalletRecordMapper walletRecordMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insertWalletRecord(WalletRecords walletRecords) {
        return walletRecordMapper.insertWalletRecord(walletRecords);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWalletRecord(Long walletCode,BigDecimal balance, Long id) throws BusinessException {
        try{
            walletRecordMapper.updateWalletRecord(walletCode);
            userMapper.updateUserBalance(balance, id);
            return 1;
        }catch (ArithmeticException e){
            throw new BusinessException("运行出错了");
        }
    }

    @Override
    public List<WalletRecords> selectWalletRecordsById(Long id) {
        return walletRecordMapper.selectWalletRecordsById(id);
    }

    @Override
    public void deleteWalletRecordByPayment() {
        walletRecordMapper.deleteWalletRecordByPayment();
    }
}
